package com.power.using.mybomb.adapter.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.power.using.mybomb.adapter.IMutlipleItem;
import com.power.using.mybomb.adapter.OnRecyclerViewListener;
import com.power.using.mybomb.base.BaseActivity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by 聪明一只哈 on 2020/3/20.
 * Time: 2020/3/20  10:18
 */
public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<BaseRecyclerHolder> {

    /**
     * 默认布局
     */
    private final int TYPE_DEFAULT=0;

    /**
     * 当list没有值的时候显示的布局
     */
    private final int TYPE_HEADER=1;

    /**
     * 多重布局
     */
    private final int TYPE_MUTIPLE=2;

    /**
     * 带header的多重布局
     */
    private final int TYPE_MUTIPLE_HEADER=3;

    protected Context context;
    protected List<T> lists;
    protected IMutlipleItem<T> items;
    protected OnRecyclerViewListener listener;

    public BaseRecyclerAdapter(Context context, Collection<T> datas, IMutlipleItem<T> items) {
        this.context = context;
        this.items = items;
        this.lists = datas==null?new ArrayList<T>():new ArrayList<T>(datas);
    }

    /**
     * 绑定数据
     * @param datas
     * @return
     */
    public BaseRecyclerAdapter<T> bindDatas(Collection<T> datas){
        this.lists=datas==null?new ArrayList<T>():new ArrayList<T>(datas);
        notifyDataSetChanged();
        return this;
    }


    /**
     * 删除数据
     * @param position
     */
    public void remove(int position){
        int more=getItemCount()-lists.size();
        lists.remove(position-more);
        notifyDataSetChanged();
    }

    /**
     * 获取指定position的item
     * @param position
     * @return
     */
    public T getItem(int position){
       int more=getItemCount()-lists.size();
       return lists.get(position-more);
    }

    /**
     * 获取总数
     * @return
     */
    public int getCount(){
        return this.lists==null?0:this.lists.size();
    }

    @NonNull
    @Override
    public BaseRecyclerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        int layoutId = items.getItemLayoutId(viewType);
        LayoutInflater inflater = LayoutInflater.from(context);
        View root = inflater.inflate(layoutId, parent, false);
        return new BaseRecyclerHolder(layoutId,root);
    }

    @Override
    public void onBindViewHolder(@NonNull BaseRecyclerHolder holder, int position) {

        int type = getItemViewType(position);
        if(type==TYPE_HEADER){
            bindView(holder,null,position);
        }else if(type==TYPE_MUTIPLE){
            bindView(holder,lists.get(position),position);
        }else if(type==TYPE_MUTIPLE_HEADER){
            int headerCount=getItemCount()-lists.size();
            bindView(holder,lists.get(position-headerCount),position);
        }else{
            bindView(holder,null,position);
        }

        holder.itemView.setOnClickListener(getOnClickListener(position));
        holder.itemView.setOnLongClickListener(getonLongClickListener(position));
    }



    @Override
    public int getItemCount() {

        if(items!=null){
            return items.getItemCount(lists);
        }
        return lists.size();
    }


    @Override
    public int getItemViewType(int position) {

        int type=getViewTypeByPosition(position);

        return super.getItemViewType(position);
    }

    /**
     * 获取指定position的布局类型
     * @param position
     * @return
     */
    private int getViewTypeByPosition(int position) {
        if(items==null){//默认布局
            return TYPE_DEFAULT;
        }else{//多布局
            if(lists!=null&&lists.size()>0){//list有值的时候
                //是否有自定义的Header
                if(getItemCount()>lists.size()){
                    int headerCount=getItemCount()-lists.size();
                    //当前位置大于header个数
                    if(position>=headerCount){
                        return TYPE_MUTIPLE_HEADER;
                    }else{//当前点击的是header
                        return TYPE_HEADER;
                    }
                }else{
                    return TYPE_MUTIPLE;
                }
            }else{//list还没有值
                return TYPE_HEADER;
            }
        }

    }

    /**
     * 设置点击/长按等事件监听器
     * @param listener
     */
    public void setOnRecyclerViewListener(OnRecyclerViewListener listener) {
        this.listener = listener;
    }

    private View.OnClickListener getOnClickListener(int position) {

        return new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(listener!=null&&view!=null){
                    listener.onItemClick(position);
                }
            }
        };
    }

    private View.OnLongClickListener getonLongClickListener(int position) {
        return new View.OnLongClickListener(){

            @Override
            public boolean onLongClick(View view) {

                if(listener!=null&&view!=null){
                    listener.onItemLongClick(position);
                }
                return true;
            }
        };
    }



    /**
     * 需实现此方法
     * @param holder
     * @param item
     */
    public abstract void bindView(BaseRecyclerHolder holder, T item, int position);

    private Toast toast;
    public void toast(final Object obj) {
        try {
            ((BaseActivity)context).runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    if (toast == null)
                        toast = Toast.makeText(context,"", Toast.LENGTH_SHORT);
                    toast.setText(obj.toString());
                    toast.show();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
